//todo: 用来管理应用状态和业务逻辑
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../models/notification_model.dart';
import '../services/push_notification_service.dart';

class NotificationState {
  final List<NotificationModel> notifications;

  NotificationState({required this.notifications});

  NotificationState copyWith({List<NotificationModel>? notifications}) {
    return NotificationState(
      notifications: notifications ?? this.notifications,
    );
  }
}

class NotificationNotifier extends StateNotifier<NotificationState> {
  final PushNotificationService pushNotificationService;

  NotificationNotifier(this.pushNotificationService)
      : super(NotificationState(notifications: []));

  void addNotification(NotificationModel notification) {
    state = state.copyWith(
      notifications: [...state.notifications, notification],
    );
    pushNotificationService.showNotification(notification);
  }
}

final notificationProvider =
    StateNotifierProvider<NotificationNotifier, NotificationState>((ref) {
  final pushNotificationService = PushNotificationService();
  return NotificationNotifier(pushNotificationService);
});
